package com.im.activity;

import java.util.Collection;

import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.Presence;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.core.sdk.core.BaseEvent;
import com.core.sdk.core.BaseFragment;
import com.core.sdk.core.LogUtil;
import com.core.sdk.core.UITask;
import com.core.sdk.ui.listview.PullToRefreshListView;
import com.core.sdk.ui.listview.PullToRefreshListView.OnRefreshListener;
import com.im.R;
import com.im.adapter.RosterEntityAdapter;
import com.im.event.PresenceEvent;
import com.im.event.UserRequestEvent;
import com.im.event.UserRequestEvent.OPERATE;
import com.im.event.UserResponseEvent;
import com.im.holder.RosterEntityStatus;
import com.im.service.SmackService;

public class TabContactListFragment extends BaseFragment implements OnItemClickListener,OnRefreshListener {
	
	PullToRefreshListView lv;
	RosterEntityAdapter adapter = null;
	
	@Override
	protected int getContentView() {
		return R.layout.fg_contact_list;
	}


	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		lv = find(R.id.fg_contact_list_lv);
		adapter = new RosterEntityAdapter(getBaesActivity());
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(this);
		lv.setOnRefreshListener(this);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loadAllUsers();
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
		RosterEntry rosterEntry = adapter.getItem(position-(lv.getHeaderViewsCount())).getData();
		startActivity(SessionActivity.createIntent(getBaesActivity(), rosterEntry));
	}
	
	
	@Override
	public void onTopRefresh() {
		loadAllUsers();
	}

	@Override
	public boolean onBottomRefresh() {
		return false;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}
	
	int loadUserCount = 0;
	@Override
	public void executeEvent(BaseEvent evt) {
		super.executeEvent(evt);
		if(evt instanceof UserResponseEvent){
			UserResponseEvent ue = (UserResponseEvent)evt;
			if(ue.getOp() == UserRequestEvent.OPERATE.request_user_list){
				procressGetUserList(ue);
			}
		}else if(evt instanceof PresenceEvent){
			PresenceEvent pe = (PresenceEvent)evt;
			Presence pp = pe.getPresence();
			int tt = pp.getType().ordinal();
			if(tt == Presence.Type.unavailable.ordinal() || tt == Presence.Type.available.ordinal() ){
				loadUserCount++;
				LogUtil.d(tag, "loadUserCount="+loadUserCount);
				loadAllUsers();
			}
		}
	}
	
	
	private void loadAllUsers(){
		UserRequestEvent evt = new UserRequestEvent(findLocation(SmackService.class),OPERATE.request_user_list);
		sendEvent(evt);
	}
	
	private void procressGetUserList(UserResponseEvent event){
		Collection<RosterEntry> list = (Collection<RosterEntry>)event.getData();
		if(list != null && list.size() > 0){
			postRunOnUi(new UITask(getBaesActivity(),list) {
				@Override
				public void run() {
					adapter.clearItems();
					Collection<RosterEntry> rosList = (Collection<RosterEntry>)getData();
					for (RosterEntry rosterEntry : rosList) {
						adapter.addItem(rosterEntry, new RosterEntityStatus());
					}
					adapter.notifyDataSetChanged();
					lv.setTopRefreshComplete();
				}
			});
		}
	}	
}
